Random Variables

Frequently, when an experiment is performed, we are interested mainly in some function of the outcome as opposed to the actual outcome itself.

For instance,
1) In recent flipping a coin experiment, we may be interested in the total number of heads that occur and not care at all about the actual Head(H)–Tail(T) sequence that results.

2) In throwing dice, we are often interested in the sum of the two dice and are not really concerned about the separate values of each die. That is, we may be interested in knowingthat the sum is 7 and may not be concerned over whether the actual outcome was: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), or (6, 1).

Also, These quantities of interest, or, more formally, these real-valued functions defined on the sample space, are known as 'Random Variables'.

Lets do an experiment with Python to demostrate

Why we need Random Variables?

& show its importance


In [64]:
%matplotlib inline
import numpy as np
import pandas as pd
from itertools import product
# from IPython.core.display import HTML
# css = open('media/style-table.css').read() + open('media/style-notebook.css').read()
# HTML('<style>{}</style>'.format(css))

In [2]:
one_toss = np.array(['H', 'T'])

In [3]:
two_tosses = list(product(one_toss, repeat=2))
two_tosses


Out[3]:
[('H', 'H'), ('H', 'T'), ('T', 'H'), ('T', 'T')]

In [4]:
# For three tosses, just change the number of repetitions:
three_tosses = list(product(one_toss, repeat=3))
three_tosses


Out[4]:
[('H', 'H', 'H'),
 ('H', 'H', 'T'),
 ('H', 'T', 'H'),
 ('H', 'T', 'T'),
 ('T', 'H', 'H'),
 ('T', 'H', 'T'),
 ('T', 'T', 'H'),
 ('T', 'T', 'T')]

As shown earlier in slide,
A probability space $(\Omega, P)$ is an outcome space accompanied by the probabilities of all the outcomes.
If you assume all eight outcomes of three tosses are equally likely, the probabilities are all 1/8:


In [5]:
three_toss_probs = (1/8)*np.ones(8)

In [6]:
three_toss_space = pd.DataFrame({
    'Omega':three_tosses,
    'P(omega)':three_toss_probs
})
three_toss_space


Out[6]:
Omega P(omega)
0 (H, H, H) 0.125
1 (H, H, T) 0.125
2 (H, T, H) 0.125
3 (H, T, T) 0.125
4 (T, H, H) 0.125
5 (T, H, T) 0.125
6 (T, T, H) 0.125
7 (T, T, T) 0.125

As you can see above, Product spaces(Probability spaces) get large very quickly. If we are tossing 10 times, the outcome space would consist of the $2^{10}$ sequences of 10 elements where each element is H or T.
The outcomes are a pain to list by hand, but computers are good at saving us that kind of pain.

Lets take example of rolling die,
If we roll a die 5 times, there are almost 8,000 possible outcomes:


In [7]:
die = np.arange(1, 7, 1)
five_rolls = list(product(die, repeat=5))
# five_rolls = [list(i) for i in product(die, repeat=5)]
five_roll_probs = (1/6**5)**np.ones(6**5)
five_roll_space = pd.DataFrame({
   'Omega':five_rolls,
    'P(omega)':five_roll_probs
})
five_roll_space


Out[7]:
Omega P(omega)
0 (1, 1, 1, 1, 1) 0.000129
1 (1, 1, 1, 1, 2) 0.000129
2 (1, 1, 1, 1, 3) 0.000129
3 (1, 1, 1, 1, 4) 0.000129
4 (1, 1, 1, 1, 5) 0.000129
5 (1, 1, 1, 1, 6) 0.000129
6 (1, 1, 1, 2, 1) 0.000129
7 (1, 1, 1, 2, 2) 0.000129
8 (1, 1, 1, 2, 3) 0.000129
9 (1, 1, 1, 2, 4) 0.000129
10 (1, 1, 1, 2, 5) 0.000129
11 (1, 1, 1, 2, 6) 0.000129
12 (1, 1, 1, 3, 1) 0.000129
13 (1, 1, 1, 3, 2) 0.000129
14 (1, 1, 1, 3, 3) 0.000129
15 (1, 1, 1, 3, 4) 0.000129
16 (1, 1, 1, 3, 5) 0.000129
17 (1, 1, 1, 3, 6) 0.000129
18 (1, 1, 1, 4, 1) 0.000129
19 (1, 1, 1, 4, 2) 0.000129
20 (1, 1, 1, 4, 3) 0.000129
21 (1, 1, 1, 4, 4) 0.000129
22 (1, 1, 1, 4, 5) 0.000129
23 (1, 1, 1, 4, 6) 0.000129
24 (1, 1, 1, 5, 1) 0.000129
25 (1, 1, 1, 5, 2) 0.000129
26 (1, 1, 1, 5, 3) 0.000129
27 (1, 1, 1, 5, 4) 0.000129
28 (1, 1, 1, 5, 5) 0.000129
29 (1, 1, 1, 5, 6) 0.000129
... ... ...
7746 (6, 6, 6, 2, 1) 0.000129
7747 (6, 6, 6, 2, 2) 0.000129
7748 (6, 6, 6, 2, 3) 0.000129
7749 (6, 6, 6, 2, 4) 0.000129
7750 (6, 6, 6, 2, 5) 0.000129
7751 (6, 6, 6, 2, 6) 0.000129
7752 (6, 6, 6, 3, 1) 0.000129
7753 (6, 6, 6, 3, 2) 0.000129
7754 (6, 6, 6, 3, 3) 0.000129
7755 (6, 6, 6, 3, 4) 0.000129
7756 (6, 6, 6, 3, 5) 0.000129
7757 (6, 6, 6, 3, 6) 0.000129
7758 (6, 6, 6, 4, 1) 0.000129
7759 (6, 6, 6, 4, 2) 0.000129
7760 (6, 6, 6, 4, 3) 0.000129
7761 (6, 6, 6, 4, 4) 0.000129
7762 (6, 6, 6, 4, 5) 0.000129
7763 (6, 6, 6, 4, 6) 0.000129
7764 (6, 6, 6, 5, 1) 0.000129
7765 (6, 6, 6, 5, 2) 0.000129
7766 (6, 6, 6, 5, 3) 0.000129
7767 (6, 6, 6, 5, 4) 0.000129
7768 (6, 6, 6, 5, 5) 0.000129
7769 (6, 6, 6, 5, 6) 0.000129
7770 (6, 6, 6, 6, 1) 0.000129
7771 (6, 6, 6, 6, 2) 0.000129
7772 (6, 6, 6, 6, 3) 0.000129
7773 (6, 6, 6, 6, 4) 0.000129
7774 (6, 6, 6, 6, 5) 0.000129
7775 (6, 6, 6, 6, 6) 0.000129

7776 rows × 2 columns

A Function on the Outcome Space

Suppose you roll a die five times and add up the number of spots you see. If that seems artificial, be patient for a moment and you'll soon see why it's interesting.

The sum of the rolls is a numerical function on the outcome space $\Omega$ of five rolls. The sum is thus a random variable. Let's call it $S$ . Then, formally, $S: \Omega \rightarrow \{ 5, 6, \ldots, 30 \}$

The range of $S$ is the integers 5 through 30, because each die shows at least one and at most six spots. We can also use the equivalent notation

$\Omega \stackrel{S}{\rightarrow} \{ 5, 6, \ldots, 30 \}$

From a computational perspective, the elements of $\Omega$ are in the column omega of five_roll_space. Let's apply this function and create a larger table.


In [8]:
five_rolls_sum = pd.DataFrame({
    'Omega':five_rolls,
    'S(omega)':five_roll_space['Omega'].map(lambda val: sum(val)),
    'P(omega)':five_roll_probs
})
five_rolls_sum


Out[8]:
Omega P(omega) S(omega)
0 (1, 1, 1, 1, 1) 0.000129 5
1 (1, 1, 1, 1, 2) 0.000129 6
2 (1, 1, 1, 1, 3) 0.000129 7
3 (1, 1, 1, 1, 4) 0.000129 8
4 (1, 1, 1, 1, 5) 0.000129 9
5 (1, 1, 1, 1, 6) 0.000129 10
6 (1, 1, 1, 2, 1) 0.000129 6
7 (1, 1, 1, 2, 2) 0.000129 7
8 (1, 1, 1, 2, 3) 0.000129 8
9 (1, 1, 1, 2, 4) 0.000129 9
10 (1, 1, 1, 2, 5) 0.000129 10
11 (1, 1, 1, 2, 6) 0.000129 11
12 (1, 1, 1, 3, 1) 0.000129 7
13 (1, 1, 1, 3, 2) 0.000129 8
14 (1, 1, 1, 3, 3) 0.000129 9
15 (1, 1, 1, 3, 4) 0.000129 10
16 (1, 1, 1, 3, 5) 0.000129 11
17 (1, 1, 1, 3, 6) 0.000129 12
18 (1, 1, 1, 4, 1) 0.000129 8
19 (1, 1, 1, 4, 2) 0.000129 9
20 (1, 1, 1, 4, 3) 0.000129 10
21 (1, 1, 1, 4, 4) 0.000129 11
22 (1, 1, 1, 4, 5) 0.000129 12
23 (1, 1, 1, 4, 6) 0.000129 13
24 (1, 1, 1, 5, 1) 0.000129 9
25 (1, 1, 1, 5, 2) 0.000129 10
26 (1, 1, 1, 5, 3) 0.000129 11
27 (1, 1, 1, 5, 4) 0.000129 12
28 (1, 1, 1, 5, 5) 0.000129 13
29 (1, 1, 1, 5, 6) 0.000129 14
... ... ... ...
7746 (6, 6, 6, 2, 1) 0.000129 21
7747 (6, 6, 6, 2, 2) 0.000129 22
7748 (6, 6, 6, 2, 3) 0.000129 23
7749 (6, 6, 6, 2, 4) 0.000129 24
7750 (6, 6, 6, 2, 5) 0.000129 25
7751 (6, 6, 6, 2, 6) 0.000129 26
7752 (6, 6, 6, 3, 1) 0.000129 22
7753 (6, 6, 6, 3, 2) 0.000129 23
7754 (6, 6, 6, 3, 3) 0.000129 24
7755 (6, 6, 6, 3, 4) 0.000129 25
7756 (6, 6, 6, 3, 5) 0.000129 26
7757 (6, 6, 6, 3, 6) 0.000129 27
7758 (6, 6, 6, 4, 1) 0.000129 23
7759 (6, 6, 6, 4, 2) 0.000129 24
7760 (6, 6, 6, 4, 3) 0.000129 25
7761 (6, 6, 6, 4, 4) 0.000129 26
7762 (6, 6, 6, 4, 5) 0.000129 27
7763 (6, 6, 6, 4, 6) 0.000129 28
7764 (6, 6, 6, 5, 1) 0.000129 24
7765 (6, 6, 6, 5, 2) 0.000129 25
7766 (6, 6, 6, 5, 3) 0.000129 26
7767 (6, 6, 6, 5, 4) 0.000129 27
7768 (6, 6, 6, 5, 5) 0.000129 28
7769 (6, 6, 6, 5, 6) 0.000129 29
7770 (6, 6, 6, 6, 1) 0.000129 25
7771 (6, 6, 6, 6, 2) 0.000129 26
7772 (6, 6, 6, 6, 3) 0.000129 27
7773 (6, 6, 6, 6, 4) 0.000129 28
7774 (6, 6, 6, 6, 5) 0.000129 29
7775 (6, 6, 6, 6, 6) 0.000129 30

7776 rows × 3 columns

Functions of Random Variables,

A random variable is a numerical function on $\Omega$ . Therefore by composition, a numerical function of a random variable is also a random variable.

For example, $S^2$ is a random variable, calculated as follows:

$S^2(\omega) = \big{(} S(\omega)\big{)}^2$

Thus for example $S^2(\text{[6 6 6 6 6]}) = 30^2 = 900$.

Events Determined by $S$

From the table five_rolls_sum it is hard to tell how many rows show a sum of 6, or 10, or any other value. To better understand the properties of $S$, we have to organize the information in five_rolls_sum.

For any subset $A$ of the range of $S$, define the event $\{S \in A\}$ as

$$ \{S \in A \} = \{\omega: S(\omega) \in A \} $$

That is, $\{ S \in A\}$ is the collection of all $\omega$ for which $S(\omega)$ is in $A$.

If that definition looks unfriendly, try it out in a special case. Take $A = \{5, 30\}$. Then $\{S \in A\}$ if and only if either all the rolls show 1 spot or all the rolls show 6 spots. So $$ \{S \in A\} = \{\text{[1 1 1 1 1], [6 6 6 6 6]}\} $$

It is natural to ask about the chance the sum is a particular value, say 10. That's not easy to read off the table, but we can access the corresponding rows:


In [9]:
five_rolls_sum[five_rolls_sum['S(omega)']==10]


Out[9]:
Omega P(omega) S(omega)
5 (1, 1, 1, 1, 6) 0.000129 10
10 (1, 1, 1, 2, 5) 0.000129 10
15 (1, 1, 1, 3, 4) 0.000129 10
20 (1, 1, 1, 4, 3) 0.000129 10
25 (1, 1, 1, 5, 2) 0.000129 10
30 (1, 1, 1, 6, 1) 0.000129 10
40 (1, 1, 2, 1, 5) 0.000129 10
45 (1, 1, 2, 2, 4) 0.000129 10
50 (1, 1, 2, 3, 3) 0.000129 10
55 (1, 1, 2, 4, 2) 0.000129 10
60 (1, 1, 2, 5, 1) 0.000129 10
75 (1, 1, 3, 1, 4) 0.000129 10
80 (1, 1, 3, 2, 3) 0.000129 10
85 (1, 1, 3, 3, 2) 0.000129 10
90 (1, 1, 3, 4, 1) 0.000129 10
110 (1, 1, 4, 1, 3) 0.000129 10
115 (1, 1, 4, 2, 2) 0.000129 10
120 (1, 1, 4, 3, 1) 0.000129 10
145 (1, 1, 5, 1, 2) 0.000129 10
150 (1, 1, 5, 2, 1) 0.000129 10
180 (1, 1, 6, 1, 1) 0.000129 10
220 (1, 2, 1, 1, 5) 0.000129 10
225 (1, 2, 1, 2, 4) 0.000129 10
230 (1, 2, 1, 3, 3) 0.000129 10
235 (1, 2, 1, 4, 2) 0.000129 10
240 (1, 2, 1, 5, 1) 0.000129 10
255 (1, 2, 2, 1, 4) 0.000129 10
260 (1, 2, 2, 2, 3) 0.000129 10
265 (1, 2, 2, 3, 2) 0.000129 10
270 (1, 2, 2, 4, 1) 0.000129 10
... ... ... ...
2635 (3, 1, 2, 2, 2) 0.000129 10
2640 (3, 1, 2, 3, 1) 0.000129 10
2665 (3, 1, 3, 1, 2) 0.000129 10
2670 (3, 1, 3, 2, 1) 0.000129 10
2700 (3, 1, 4, 1, 1) 0.000129 10
2810 (3, 2, 1, 1, 3) 0.000129 10
2815 (3, 2, 1, 2, 2) 0.000129 10
2820 (3, 2, 1, 3, 1) 0.000129 10
2845 (3, 2, 2, 1, 2) 0.000129 10
2850 (3, 2, 2, 2, 1) 0.000129 10
2880 (3, 2, 3, 1, 1) 0.000129 10
3025 (3, 3, 1, 1, 2) 0.000129 10
3030 (3, 3, 1, 2, 1) 0.000129 10
3060 (3, 3, 2, 1, 1) 0.000129 10
3240 (3, 4, 1, 1, 1) 0.000129 10
3890 (4, 1, 1, 1, 3) 0.000129 10
3895 (4, 1, 1, 2, 2) 0.000129 10
3900 (4, 1, 1, 3, 1) 0.000129 10
3925 (4, 1, 2, 1, 2) 0.000129 10
3930 (4, 1, 2, 2, 1) 0.000129 10
3960 (4, 1, 3, 1, 1) 0.000129 10
4105 (4, 2, 1, 1, 2) 0.000129 10
4110 (4, 2, 1, 2, 1) 0.000129 10
4140 (4, 2, 2, 1, 1) 0.000129 10
4320 (4, 3, 1, 1, 1) 0.000129 10
5185 (5, 1, 1, 1, 2) 0.000129 10
5190 (5, 1, 1, 2, 1) 0.000129 10
5220 (5, 1, 2, 1, 1) 0.000129 10
5400 (5, 2, 1, 1, 1) 0.000129 10
6480 (6, 1, 1, 1, 1) 0.000129 10

126 rows × 3 columns

There are 126 values of $\omega$ for which $S(\omega) = 10$. Since all the $\omega$ are equally likely, the chance that $S$ has the value 10 is 126/7776.

We are informal with notation and write $\{ S = 10 \}$ instead of $\{ S \in \{10\} \}$: $$ P(S = 10) = \frac{126}{7776} = 1.62\% $$

This is how Random Variables help us quantify the results of experiments for the purpose of analysis.

i.e., Random variables provide numerical summaries of the experiment in question. - Stats110 harvard (also below paragraph)

This definition is abstract but fundamental; one of the most important skills to develop when studying probability and statistics is the ability to go back and forth between abstract ideas and concrete examples. Relatedly, it is important to work on recognizing the essential pattern or structure of a problem and how it connectsto problems you have studied previously. We will often discuss stories that involve tossing coins or drawing balls from urns because they are simple, convenient sce- narios to work with, but many other problems are isomorphic: they have the same essential structure, but in a different guise.

we can use mathematical opeartion on these variables since they are real valued function nowto problems you have studied previously. We will often discuss stories that involve tossing coins or drawing balls from urns because they are simple, convenient sce- narios to work with, but many other problems are isomorphic: they have the same essential structure, but in a di↵erent guise.

Looking at Distributions

The table below shows all the possible values of $S$ along with all their probabilities. It is called a "Probability Distribution Table" for $S$ .


In [119]:
dist_S = five_rolls_sum.drop('Omega', axis=1).groupby('S(omega)', as_index=False).sum()
dist_S


Out[119]:
S(omega) P(omega)
0 5 0.000129
1 6 0.000643
2 7 0.001929
3 8 0.004501
4 9 0.009002
5 10 0.016204
6 11 0.026363
7 12 0.039223
8 13 0.054012
9 14 0.069444
10 15 0.083719
11 16 0.094522
12 17 0.100309
13 18 0.100309
14 19 0.094522
15 20 0.083719
16 21 0.069444
17 22 0.054012
18 23 0.039223
19 24 0.026363
20 25 0.016204
21 26 0.009002
22 27 0.004501
23 28 0.001929
24 29 0.000643
25 30 0.000129

The contents of the table – all the possible values of the random variable, along with all their probabilities – are called the probability distribution of $S$ , or just distribution of $S$ for short. The distribution shows how the total probability of 100% is distributed over all the possible values of $S$ .

Let's check this, to make sure that all the $\omega$ 's in the outcome space have been accounted for in the column of probabilities.


In [123]:
dist_S.ix[:,1].sum()


Out[123]:
0.99999999999999911

That's 1 in a computing environment, and it is true in general for the distribution of any random variable.

Probabilities in a distribution are non-negative and sum to 1.

Visualising Distribution


In [126]:
dist_S.ix[:,0], dist_S.ix[:,1]


Out[126]:
(0      5
 1      6
 2      7
 3      8
 4      9
 5     10
 6     11
 7     12
 8     13
 9     14
 10    15
 11    16
 12    17
 13    18
 14    19
 15    20
 16    21
 17    22
 18    23
 19    24
 20    25
 21    26
 22    27
 23    28
 24    29
 25    30
 Name: S(omega), dtype: int64, 0     0.000129
 1     0.000643
 2     0.001929
 3     0.004501
 4     0.009002
 5     0.016204
 6     0.026363
 7     0.039223
 8     0.054012
 9     0.069444
 10    0.083719
 11    0.094522
 12    0.100309
 13    0.100309
 14    0.094522
 15    0.083719
 16    0.069444
 17    0.054012
 18    0.039223
 19    0.026363
 20    0.016204
 21    0.009002
 22    0.004501
 23    0.001929
 24    0.000643
 25    0.000129
 Name: P(omega), dtype: float64)

In [127]:
s = dist_S.ix[:,0]
p_s = dist_S.ix[:,1]

In [136]:
dist_S = pd.concat([s,p_s],axis=1)
dist_S


Out[136]:
S(omega) P(omega)
0 5 0.000129
1 6 0.000643
2 7 0.001929
3 8 0.004501
4 9 0.009002
5 10 0.016204
6 11 0.026363
7 12 0.039223
8 13 0.054012
9 14 0.069444
10 15 0.083719
11 16 0.094522
12 17 0.100309
13 18 0.100309
14 19 0.094522
15 20 0.083719
16 21 0.069444
17 22 0.054012
18 23 0.039223
19 24 0.026363
20 25 0.016204
21 26 0.009002
22 27 0.004501
23 28 0.001929
24 29 0.000643
25 30 0.000129

In [149]:
dist_S.plot(x="S(omega)",y="P(omega)", kind="bar")


Out[149]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa37c678dd8>

In [166]:
from prob140 import Plot

In [165]:
!pip install sympy


Collecting sympy
  Downloading sympy-1.1.1.tar.gz (4.6MB)
    100% |████████████████████████████████| 4.6MB 85kB/s eta 0:00:011
Collecting mpmath>=0.19 (from sympy)
  Downloading mpmath-0.19.tar.gz (498kB)
    100% |████████████████████████████████| 501kB 98kB/s eta 0:00:01
Building wheels for collected packages: sympy, mpmath
  Running setup.py bdist_wheel for sympy ... done
  Stored in directory: /home/nipun/.cache/pip/wheels/86/b6/ef/72760cc52418901e59f20a85454243f2c9503a8f89b5501f5f
  Running setup.py bdist_wheel for mpmath ... done
  Stored in directory: /home/nipun/.cache/pip/wheels/02/2b/99/cd867d5da48d951118a8020e86c0c12a65022702426582d4b8
Successfully built sympy mpmath
Installing collected packages: mpmath, sympy
Successfully installed mpmath-0.19 sympy-1.1.1

In [ ]: